<template>
  <d2-container>
    <div class="pb-2 ml-2">
      <div><el-button type="warning" @click="addNatureDialog" icon="el-icon-plus">添加性质</el-button></div>
    </div>
    <div class="flex-1 ml-2">
      <el-table :data="merchantNatureList" stripe>
        <el-table-column align="center" type="selection" width="55" />
        <el-table-column align="center" label="序号" type="index" width="65" />
        <el-table-column align="center" label="性质名称" prop="nature_name" width="130" />
        <el-table-column align="center" label="性质描述" prop="nature_describe" width="130" />
        <el-table-column align="center" label="操作" width="120">
          <template slot-scope="scope">
            <el-button size="mini" @click="natureEdit(scope.row.id)">修改</el-button>
            <el-button size="mini" type="danger" @click="openDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="flex-1 mt-3">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page.sync="queryInfo.page" layout="total, prev, pager, next" :total="natureTotal">
        </el-pagination>
      </div>
    </div>
    <!-- 综合 -->
    <el-dialog :visible.sync="dialogNatureVisible" width="30%" title="添加/修改综合">
      <el-form :model="formNature" ref="form" label-width="80px">
        <el-form-item label="店铺等级" prop="nature_name">
          <div style="width: 50%;">
            <el-input v-model="formNature.nature_name"></el-input>
          </div>
        </el-form-item>
        <el-form-item label="等级描述" prop="nature_describe">
          <div style="width: 80%;">
            <el-input type="textarea" v-model="formNature.nature_describe" rows="4"></el-input>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogNatureVisible = false">取 消</el-button>
        <el-button type="primary" v-if="!addNatureState" @click="submitNatureForm">修改</el-button>
        <el-button type="primary" v-if="addNatureState" @click="addNatureForm">保存</el-button>
      </div>
    </el-dialog>
  </d2-container>
</template>

<script>
  import {
    merchantnatureDeleteUrl,
    merchantnatureUpdateUrl,
    merchantnatureCreateUrl,
    merchantnatureDetailUrl,
    merchantnatureListUrl,
  } from "@/utils/merchant"
  export default {
    data() {
      return {
        dialogNatureVisible: false,
        addNatureState: true,
        queryInfo: {
          page: 1,
          pageSize: 10
        },
        formNature: {
          nature_name: '',
          nature_describe: ''
        },
        merchantNatureList: [],
        natureTotal: 1,
        getNatureId: ''
      }
    },
    created() {
      this.getMerchantNatureList()
    },
    methods: {
      getMerchantNatureList() {
        this.$get(merchantnatureListUrl, this.queryInfo).then(res => {
          if (res.data.code == 200) {
            this.merchantNatureList = res.data.data.list
            this.natureTotal = res.data.data.total
          }
        })
      },
      // 分页页码显示条数
      handleSizeChange(newSize) {
        // 把每页显示多少数据重新赋值
        this.queryInfo.pageSize = newSize
        this.getMerchantNatureList()
      },
      // 翻页事件
      handleCurrentChange(newPage) {
        this.queryInfo.page = newPage
        this.getMerchantNatureList()
      },
      addNatureDialog() {
        this.dialogNatureVisible = true
        this.addNatureState = true
      },
      addNatureForm() {
        this.$post(merchantnatureCreateUrl, this.formNature).then(res => {
          if (res.data.code == 200) {
            this.dialogNatureVisible = false
            this.$message({
              type: 'success',
              message: '添加成功!'
            });
            this.getMerchantNatureList()
            this.formNature = {}
          }
        })
      },
      natureEdit(natureId) {
        this.dialogNatureVisible = true
        this.addNatureState = false,
          this.getNatureId = natureId
        this.$get(merchantnatureDetailUrl + natureId + '/').then(res => {
          if (res.data.code == 200) {
            this.formNature = {
              nature_name: res.data.data.nature_name,
              nature_describe: res.data.data.nature_describe
            }
          }
        })
      },
      submitNatureForm() {
        this.$put(merchantnatureUpdateUrl + this.getNatureId + '/', this.formNature).then(res => {
          if (res.data.code == 200) {
            this.dialogNatureVisible = false
            this.$message({
              type: 'success',
              message: '添加成功!'
            });
            this.getMerchantNatureList()
            this.formNature = {}
          }
        })
      },
      openDelete(natureId) {
        this.$confirm('此操作将删除该类目?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$del(merchantnatureDeleteUrl + natureId + '/').then(res => {
            if (res.data.code == 200) {
              this.$message({
                type: 'danger',
                message: '删除成功!'
              });
              this.getMerchantNatureList()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
    }
  }
</script>

<style>
</style>